<?php
/**
* PHP Template.
*/
class EventsModel extends Model {
	//private $user = "calendar@wahomebrewers.org";
	//private $pass = "calendar-waha";
	private $CalendarBaseURL = "http://www.google.com/calendar/feeds/default/";
	private $user = "mike@ensor.cc";
	private $pass = "1nsightS";
	private $gDataCal = null;
	private $calList = null;

	public function __construct() {
		parent::Model();
		$this->load->library("errors");
	}
	/**
	 *	Function returns ID for calendar to be used by setUser
	 */
	private function getCalendarIdByName($name) {
		$cal = $name; // "default" by default
		if($this->getCalendarList()==null) {
			// have an error...escape out?
			return $cal;
		}
		foreach($this->getCalendarList() as $calendar) {
			if($calendar->title == $name) {
				$pos = strlen($this->CalendarBaseURL);
				$cal = substr($calendar->id,$pos);
				break;
			}
		}
		return $cal;
	}
	public function getSingleEvent($calUserName,$eventId) {
		$query = $this->getQueryObj($calUserName);
		$query->setEvent($eventId);
		$eventFeed = $service->getCalendarEventFeed($query);
		return $eventFeed;
	}
	private function getCalendarReference($calUserName='default') {
		Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
		Zend_Loader::loadClass('Zend_Gdata_Calendar');
		if($this->gDataCal==null) {
			$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
			$client = Zend_Gdata_ClientLogin::getHttpClient($this->user, $this->pass, $service);
			$this->gDataCal = new Zend_Gdata_Calendar($client);
		}
		//$this->gDataCal->setUser($this->getCalendarIdByName($calUserName)); //does not work!!!
		return $this->gDataCal;
	}
	private function getQueryObj($calUserName="default",$calVisibility="private",$projection="full",$orderBy="starttime") {
		$service = $this->getCalendarReference(); // returns back single calendar
		$query = $service->newEventQuery();
		$query->setUser($this->getCalendarIdByName($calUserName));
		$query->setVisibility($calVisibility);
		$query->setProjection($projection);
		$query->setOrderby($orderBy);
		return $query;
	}
	private function fillCalendarFeedList() {
		try {
			$listFeed = $this->getCalendarReference()->getCalendarListFeed();
		} catch (Zend_Gdata_App_Exception $e) {
			echo "ERROR:::" . $e->getMessage() . "<br/>";
			return null;
		}
		$this->setCalendarList($listFeed);
	}
	private function setCalendarList($calList){
		$this->calList = $calList;
	}
	private function getCalendarList(){
		return $this->calList;
	}
	/**************************************** PUBLIC FUNCTIONS **************************************/
	public function createEvent($calUserName='default',$title,$desc="",$startDate,$startTime="",$endDate="",$endTime="",$where="",$tzOffset="-07")
	{
		$gc = $this->getCalendarReference();
		$newEntry = $gc->newEventEntry();
		$newEntry->title = $gc->newTitle(trim($title));
		$newEntry->where  = array($gc->newWhere($where));

		$newEntry->content = $gc->newContent($desc);
		$newEntry->content->type = 'text';

		$when = $gc->newWhen();
		$when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
		$when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
		$newEntry->when = array($when);

		$createdEntry = $gc->insertEvent($newEntry);
		return $createdEntry->id->text;
	}

	public function getEventsByKeyword($calUserName,$keyword="default") {
		try {
			$query = $this->getQueryObj($calUserName);
			$query->setQuery($keyword);
			$eventFeed = $this->getCalendarReference()->getCalendarEventFeed($query);
		} catch (Zend_Gdata_App_Exception $e) {
			echo "ERROR:::" . $e->getMessage() . "<br/>";
			return null;
		}
		return $eventFeed;
	}
	public function getEventsList($calUserName,$start,$end) {
		$query = $this->getQueryObj($calUserName);
		$query->setStartMin($start);
		$query->setStartMax($end);
		try {
			$eventFeed = $this->getCalendarReference()->getCalendarEventFeed($query);
		} catch (Zend_Gdata_App_Exception $e) {
			echo "ERROR:::" . $e->getMessage() . "<br/>";
			return null;
		}
		return $eventFeed;
	}
	public function getPublicCalendarList(){
		if($this->getCalendarList()==null)
		$this->fillCalendarFeedList();
		return $this->getCalendarList();
	}
}
?>